package net.oschina.app.fragment.favorite;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import net.oschina.app.R;
import net.oschina.app.adapter.my.FavoriteRecyclerAdapter;
import net.oschina.app.bean.Favorite;
import net.oschina.app.bean.FavoriteList;
import net.oschina.app.constant.Constants;
import net.oschina.app.fragment.BaseFragment;
import net.oschina.app.listener.OnRecycleViewItemClickListener;
import net.oschina.app.ui.detail.SoftDetailActivity;
import net.oschina.app.util.UIUtils;
import net.oschina.app.util.URLGet;

import java.util.List;

/**
 * Created by YL on 2017/4/1.
 */

public abstract class BasefavoriteFragment extends BaseFragment {

    private XRecyclerView mView;
    private List<Favorite> mFavoritelist;

    @Override
    protected View initView() {
        mView = (XRecyclerView) View.inflate(UIUtils.getContext(), R.layout.f_software_fragment, null);
        mView.setLayoutManager(new LinearLayoutManager(getActivity()));
        return mView;
    }

    @Override
    protected void setData() {
        String fsoftPath = Constants.Favorite_Path + getPosition() + Constants.End_Path;

        URLGet urlGet = new URLGet();
        urlGet.getData(fsoftPath, FavoriteList.class, new URLGet.OnURLGetLitener<FavoriteList>() {
            @Override
            public void showSuccess(FavoriteList bean) {
                mFavoritelist = bean.getFavoritelist();
                dealCommonSuccess(mFavoritelist);
            }

            @Override
            public void showError(Exception e) {
            }
        });
    }

    protected abstract String getPosition();

    @Override
    protected void showSuccess() {
        FavoriteRecyclerAdapter adapter = new FavoriteRecyclerAdapter(mFavoritelist);
        mView.setAdapter(adapter);
        if (mFavoritelist != null && mFavoritelist.size() > 0) {
            View footView = View.inflate(UIUtils.getContext(), R.layout.my_recycler_foot_view, null);
            mView.setFootView(footView);
        }
        if(getPosition().equals("1")) {
            adapter.setOnItemClickListener(new OnRecycleViewItemClickListener() {
                @Override
                public void onClick(View view, int position) {
                    position = position - 1;
                    Intent intent = new Intent(getActivity(), SoftDetailActivity.class);
                    intent.putExtra("url", mFavoritelist.get(position).getUrl());
                    startActivity(intent);
                }
            });
            changeView(mView);
        }
    }
}
